// formPackage/components/newsItem/newsItem.js
import { VIDEO_URL } from "../../service/base_service"
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    newsItem: {
      type: Object,
      value: {}
    },
    isDetail: {
      type: Boolean,
      value: false,
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    videoJoint: VIDEO_URL
  },

  /**
   * 组件的方法列表
   */
  methods: {
   // 跳转到详情
    pushToDetail(){
      if(this.isDetail){
        return;
      }
      wx.navigateTo({
        url: '/shopMallPackage/pages/newDynDetail/newDynDetail?id=' + this.properties.newsItem.id,
      })
    },
    // 点赞动态:
    approveDynAction(){
       this.triggerEvent('approveAction',{id:this.properties.newsItem.id})
    },

    //预览图片或者视频:
    previewImgs(e) {
      let index = e.currentTarget.dataset.index;
      let images = this.properties.newsItem.images;
      let item = images[index];
      if (item.fileType == 2) {
        //如果是视频:
        app.globalData.videoUrl = item.url;
        wx.navigateTo({
          url: '/pages/videoPlay/videoPlay',
        })
        return;
      }
      let urls = [];
      images.forEach((i) => {
        if (i.fileType == 1) {
          urls.push(i.url)
        } else {
          urls.push(i.url + this.data.videoJoint);
        }
      })
      wx.previewImage({
        current: item.url,
        urls: urls,
      })
    },

    //跳转到聊天:
    pushToChat() {
      let item = this.properties.newsItem;
      let chatData = {
        name: item.name || item.nickName,
        avatar: item.avatar,
        chatUserId: item.openId,
        conversationType: "C2C",
        houseNumber: item.houseInfo.houseNumber || "",
        houseName: item.houseInfo.name || "",
        expertId: item.id,
        expertPhone: item.phone || "",
        expertWeChat: item.wxChat || "",
      }
      app.globalData.chatData = chatData;
      wx.navigateTo({
        url: '/firstPackage/pages/msgModule/chatRoom/chatRoom',
      })
    },
    //跳转到新房详情:
    pushToHouseDetail() {
      let item = this.properties.newsItem;
      let houseNumber = item.houseNumber || item.houseInfo.houseNumber;
      wx.navigateTo({
        url: '/newHousePackage/pages/newHouseDetail/newHouseDetail?houseNumber=' + houseNumber,
      })

    },
    copyWxChatAction(e) {
      let text = e.currentTarget.dataset.text;
      wx.setClipboardData({
        data: text,
        success: function (res) {
          wx.getClipboardData({
            success: function (res) {
              wx.showToast({
                title: '复制微信号成功'
              })
            },
            fail: function (err) {
              wx.showToast({
                title: '复制失败',
                icon:"none"
              })
            }
          })
        }
      })
    },

  },

})
